package com.lsh.controller;


import com.lsh.pojo.PageBean;
import com.lsh.pojo.Result;
import com.lsh.pojo.student;
import com.lsh.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/stu-management")
public class StuController {

    @Autowired
    private StuService stuService;

    //    查询所有员工
    @GetMapping
    public Result list() {
        List<student> studentList = stuService.list();
        return Result.success(studentList);
    }

    //    分页查询
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
//        log.info("分页查询，参数{}，{}",page,pageSize);
        PageBean pageBean = stuService.page(page, pageSize);
        return Result.success(pageBean);
    }

    //    根据学号查询
    @GetMapping("/getById")
    public Result getById(@RequestParam String sno) {
//        log.info("根据学号查询员工信息，id{}", sno);
        student stu = stuService.getById(sno);
        if (stu == null) {
            return Result.error("未找到该学生！");
        }
        return Result.success(stu);
    }

    //新增学生
    @PostMapping("/addStu")
    public Result addStu(@RequestBody student stu) {
//        log.info("新增员工：{}", stu);
        stuService.addStu(stu);
        return Result.success();
    }

    //    修改学生信息
    @PutMapping("/updateStu")
    public Result update(@RequestBody student stu) {
//        log.info("跟新员工信息：{}", stu);
        stuService.update(stu);
        return Result.success();
    }

    //删除学生信息
    @DeleteMapping("/deleteStu")
    public Result delete(@RequestParam String sno) {
        log.info("删除学生信息：sno:{}",sno);
        stuService.delete(sno);
        return Result.success();
    }

}
